﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Business
{
    /// <summary>
    /// 支付宝app支付
    /// </summary>
    public class AlipayBiz
    {
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <returns></returns>
        private static DefaultAopClient AopClient()
        {
            string appid = ConfigHelper.AppSetting<string>("zfb_appid");
            string postUrl = ConfigHelper.AppSetting<string>("zfb_posturl");
            string privateKey = ConfigHelper.AppSetting<string>("zfb_privatekey");
            string publicKey = ConfigHelper.AppSetting<string>("zfb_publickey");
            DefaultAopClient client = new DefaultAopClient(postUrl, appid, privateKey, "json", "1.0", "RSA2", publicKey, "UTF-8");
            return client;
        }

        /// <summary>
        /// 提交支付宝网关支付
        /// tuple item1 是否成功 item2 成功返回Body item3失败返回错误原因
        /// </summary>
        /// <returns></returns>
        public static Tuple<bool, string, string> DoSubmit(string orderId, decimal amt)
        {
            DefaultAopClient client = AopClient();
            string notityUrl = ConfigHelper.AppSetting<string>("zfb_notifyurl");
            //组装业务参数model
            AlipayTradeAppPayModel appModel = new AlipayTradeAppPayModel();
            appModel.Body = "爱电子-充值";
            appModel.Subject = "爱电子-充值";               //商品的标题/交易标题/订单标题/订单关键字等。
            appModel.OutTradeNo = orderId;                  //商户网站唯一订单号
            appModel.TimeoutExpress = "30m";
            appModel.TotalAmount = amt.ToString();          //订单总金额，单位为元，精确到小数点后两位
            appModel.ProductCode = "QUICK_MSECURITY_PAY";   //销售产品码，商家和支付宝签约的产品码，为固定值

            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            request.SetNotifyUrl(notityUrl);        //设置支付完成异步通知接收地址
            request.SetBizModel(appModel);             //将业务model载入到request
            try
            {
                LogHelper.Info("提交支付宝网关支付,请求数据：" + JsonHelper.Serialize(request));

                var response = client.SdkExecute(request);
                LogHelper.Info("提交支付宝网关支付,响应数据：" + JsonHelper.Serialize(response));
                if (!response.IsError)
                    return new Tuple<bool, string, string>(true, response.Body, "");
                else
                    return new Tuple<bool, string, string>(false, "", response.Msg);
            }
            catch (Exception ex)
            {
                LogHelper.Error("提交支付宝网关支付,异常原因：" + ex.ToString());
                return new Tuple<bool, string, string>(false, "", "提交支付宝网关支付异常");
            }
        }

        /// <summary>
        /// 查询订单
        /// </summary>
        /// <returns></returns>
        public static AlipayTradeQueryResponse QueryOrder(string orderId)
        {
            DefaultAopClient client = AopClient();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();
            model.OutTradeNo = orderId;
            model.TradeNo = "";

            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            request.SetBizModel(model);
            AlipayTradeQueryResponse response = null;
            try
            {
                response = client.Execute(request);
                return response;
            }
            catch (Exception exp)
            {
                LogHelper.Error("支付宝查询订单异常" + exp.ToString());
            }
            return response;
        }

        /// <summary>
        /// 退款
        /// </summary>
        /// <returns></returns>
        public static Tuple<bool, string> Refund(string orderId, decimal refundAmt)
        {
            DefaultAopClient client = AopClient();
            //组装业务参数model
            AlipayTradeRefundModel refundModel = new AlipayTradeRefundModel();
            refundModel.OutTradeNo = orderId;
            refundModel.RefundAmount = refundAmt.ToString();

            AlipayTradeRefundRequest req = new AlipayTradeRefundRequest();
            req.SetBizModel(refundModel);
            try
            {
                LogHelper.Info("支付宝退款,请求数据：" + JsonHelper.Serialize(req));

                var response = client.Execute(req);
                LogHelper.Info("支付宝退款,响应数据：" + JsonHelper.Serialize(response));
                if (!response.IsError && response.Code == "10000")
                    return new Tuple<bool, string>(true, "退款成功");
                else
                    return new Tuple<bool, string>(false, response.Msg);
            }
            catch (Exception ex)
            {
                LogHelper.Error("支付宝退款,异常原因：" + ex.ToString());
                return new Tuple<bool, string>(false, "支付宝退款异常");
            }
        }
    }
}
